<?php
class PublicAction extends SystemCommonAction {

    public function verify() {
        $type	 =	 isset($_GET['type'])?$_GET['type']:'gif';
        import("@.ORG.Image");
        Image::buildImageVerify(4,2,$type,145,20);
    }

    public function doLogin() {

        $adminUser = D('AdminUser');

        if ($adminUser->create()) {

            if($_SESSION['verify'] != md5(strtoupper($_POST['verify']))) {
                $this->assign('jumpUrl',__APP__.'/Admin/Index/login');
                $this->success('验证码错误！');
            }

            $array_user = $adminUser->where('user_name=\''.$_POST['user_name'].'\'')->find();

            if(count($array_user) > 0) {
                if ($array_user['password'] != md5($_POST['password'])) {
                    $this->assign('jumpUrl',__APP__.'/Admin/Index/login');
                    $this->error('密码错误！');
                }

                if ($array_user['is_close'] == 1) {
                    $this->assign('jumpUrl',__APP__.'/Admin/Index/login');
                    $this->success('该管理员已被锁定！');
                }

    			/*管理员信息装入session*/
                Session::setLocal('s_admin'     , 1);
                Session::setLocal('name'        , $array_user['user_name']);
                Session::setLocal('id'          , $array_user['user_id']);
                Session::setLocal('action_list' , $array_user['action_list']);

                //                if (false !== $adminUser->updateUser(array(),$array_user['user_id'])) {
                redirect(__APP__.'/Admin/Index/index');
            //                } else {
            //                	$this->assign('jumpUrl',__APP__.'/Admin/Index/login');
            //                    $this->error('登录失败,请稍后尝试！');
            //                }
            } else {
                $this->assign('jumpUrl',__APP__.'/Admin/Index/login');
                $this->success('此管理员不存在！');
            }
        } else {
            $this->assign('jumpUrl',__APP__.'/Admin/Index/login');
            $this->success($adminUser->getError());
        }
    }

    public function top() {
        $this->display('top');
    }

    public function menu() {
        $modules = F('inc_menu');
        $purview = F('inc_priv');
        //		foreach ($modules AS $key => $value)
        //	    {
        //	        ksort($modules[$key]);
        //	    }
        //	    ksort($modules);

        foreach ($modules AS $key => $val) {
            $menus[$key]['label'] = L($key);

            if (is_array($val)) {
                foreach ($val AS $k => $v) {
                    if (isset($purview[$k])) {
                        if (is_array($purview[$k])) {
                            $boole = false;
                            foreach ($purview[$k] as $action) {
                                $boole = $boole || $this->admin_priv($action, '', false);
                            }
                            if (!$boole) {
                                continue;
                            }
                        } else {
                            if (!$this->admin_priv($purview[$k], '', false)) {
                                continue;
                            }
                        }
                    }
                    $menus[$key]['children'][$k]['label']  = L($k);
                    $menus[$key]['children'][$k]['action'] = $v;
                }

            } else {
                $menus[$key]['action'] = $val;
            }

            // 如果children的子元素长度为0则删除该组
            if(empty($menus[$key]['children'])) {
                unset($menus[$key]);
            }
        }
        $this->assign('menus',$menus);
        $this->display('menu');
    }

    public function drag() {
        $this->display('drag');
    }

    public function main() {
        $periods = F('period');

        $periods = array_reverse($periods);

        $this->assign('periods',$periods);
        $this->display('main');
    }
}
?>